package Leetcode第一期;

/**
 * @author : K k
 * @date : 10:54 2020/5/6
 */
public class 反转链表_24 {

    //双指针
    public ListNode reverseList1(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode tmp = null;

        while (cur!=null){
            tmp=cur.next; //存放遍历的下一个节点
            cur.next=pre; //下一个节点为前一节点
            pre=cur;
            cur=tmp;
        }
        return pre;
    }

    //递归
    public ListNode reverseList2(ListNode head) {
       if (head == null || head.next==null) {return head;}
       ListNode ret=reverseList2(head.next);
       head.next.next=head;
       head.next=null;
       return ret;//返回最后一个节点
    }


    public static void main(String[] args) {

    }

    public static void print(ListNode node){
        while (node!=null){
            System.out.println(node.val);
            node=node.next;
        }
    }
}
